#!/usr/bin/env gosh
(use sxml.ssax)
(use sxml.sxpath)
(use sxml.tools)
(use sxml.serializer)
(use gauche.parseopt)

(define (main args)
  (let-args (cdr args)
      ((option-f "f|f=s" #f)
       . rest)
    (unless option-f (print "usage: xsl -f=XSL_PATH XML_FILE") (exit 1))
    (let* ((sxml (if (null? rest)
		   (ssax:xml->sxml (current-input-port) '())
		   (call-with-input-file (car rest) (cut ssax:xml->sxml <> '()))))
	   (new-sxml `(*TOP* ,@((take-until (ntype?? '*)) (sxml:child-nodes sxml))
			     (*PI* xml-stylesheet ,(format #f "type=\"text/xsl\" href=\"~a\"" option-f))
			     ,@((sxpath "/*[1]") sxml))))
      (srl:sxml->xml new-sxml (current-output-port))))
  (exit 0))

